package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"log"
	"os"
	"os/signal"
	"syscall"
	"webProxy/pkg/data"
	phttp "webProxy/pkg/http"
)

var (
	port       = flag.Int64("p", 8001, "http监听端口")
	broadPorts = flag.String("bp", "[]", "proxy端口json数组, [123,456]")
)

func main() {

	flag.Parse()
	log.Printf("port:%d, broadPort:%s", *port, *broadPorts)
	initBiz()

	// signal
	sigs := make(chan os.Signal, 1)
	signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
	sig := <-sigs
	log.Printf("recv signal:%s, exit now!\n", sig.String())
}

func initBiz() {
	proxyPort := make([]int64, 0)
	if err := json.Unmarshal([]byte(*broadPorts), &proxyPort); err != nil {
		log.Printf("proxyPort error, err:%s", err.Error())
		os.Exit(1)
	}
	data.Init(proxyPort)

	httpAddr := fmt.Sprintf("0.0.0.0:%d", *port)
	phttp.StartHttp(httpAddr)
}
